//
//  XJShouCangTopicViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/14.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  我的收藏 > 收藏的帖子

#import "XJShouCangTopicViewController.h"
#import "XJYMTTeiZiDetailViewController.h" //有名堂 帖子详情

#import "XJShouCangTopicsModel.h"
#import "XJSCTopicsCell.h"
#import "XJSCTopicsFormat.h"
#import "XJSCTopicTableViewProxy.h"

#import "XJBottomView.h" //底部全选删除按钮


@interface XJShouCangTopicViewController ()<XJSCTopicsFormatDelegate>

/**tableView*/
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) XJSCTopicsFormat *topicsFormat;
@property (nonatomic, strong) XJSCTopicTableViewProxy *topicsProxy;
@property (nonatomic, strong) XJBottomView *bottomView;

@end

@implementation XJShouCangTopicViewController

#pragma mark - 一 lazy
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        
        UINib *nib = [UINib nibWithNibName:@"XJSCTopicsCell" bundle:nil];
        [_tableView registerNib:nib forCellReuseIdentifier:@"XJSCTopicsCell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; //去掉分割线
        _tableView.delegate = self.topicsProxy;
        _tableView.dataSource = self.topicsProxy;
        
        _tableView.tableFooterView = [UITableView new];
    
    }
    return _tableView;
}

//处理业务逻辑
- (XJSCTopicsFormat *)topicsFormat {
    if (!_topicsFormat) {
        _topicsFormat = [[XJSCTopicsFormat alloc] init];
        _topicsFormat.delegate = self;
    }
    return _topicsFormat;
}

//处理tableView的代理方法。(cell赋值、点击事件)
- (XJSCTopicTableViewProxy *)topicsProxy {
    if (!_topicsProxy) {
        _topicsProxy = [[XJSCTopicTableViewProxy alloc] init];
        
        
        XJWeakSelf(self);
        _topicsProxy.sctopicsTableViewProxySelectedCellBlock = ^(NSIndexPath *indexPath, BOOL xx_isEditing, BOOL xx_isSelected, XJShouCangTopicsModel *selModel) {
          
            XJLog(@"选中状态:%@",xx_isSelected ? @"YES": @"NO");
            if (xx_isEditing) {
                //                XJLog(@"编辑状态 %ld",indexPath.row);
                [weakself.topicsFormat selectTopicAtIndexPath:indexPath isSelected:xx_isSelected];
            }else {
                
                XJLog(@"非xx 跳转,%ld",indexPath.row);
                XJYMTTeiZiDetailViewController *ymtTieDetailVC = [[XJYMTTeiZiDetailViewController alloc] init];
                ymtTieDetailVC.tieZiId = selModel.invitation_id;
                ymtTieDetailVC.type = selModel.invitation_type;
                
                [weakself.navigationController pushViewController:ymtTieDetailVC animated:YES];
            }
        };
    }
    return _topicsProxy;
}

- (XJBottomView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[XJBottomView alloc] initWithFrame:CGRectMake(0, KHeight, KWidth, 50)];
        [self.view addSubview:_bottomView];
        
        XJWeakSelf(self);
        _bottomView.xjAllSeletedBlock = ^(BOOL btnSelected) {
            XJLog(@"%@",(btnSelected ? @"全选" : @"否"));
            [weakself.topicsFormat selectAllTopicWithStatus:btnSelected];
        };
        
        _bottomView.xjDeleBlock = ^{
            XJLog(@"点击删除按钮");
            [weakself.topicsFormat beginToDeleteSeletedTopics];
        };
    }
    return _bottomView;
}

#pragma mark - 二 生命周期


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor grayColor];
    
    
    CGRect tempFrame = self.view.bounds;
    tempFrame.size.height = (KHeight - 40 - kStatusBarAndNavigationBarHeight);
    self.tableView.frame = tempFrame;
    [self.view addSubview:self.tableView];
    
    //调用这个方法会自动触发 XJSCTopicsFormatDelegate 对应的代理方法
    [self.topicsFormat requestSCTopicsData];
}


#pragma mark - 三 XJSCTopicsFormatDelegate
- (void)requestSCTopicsListSuccessWithArray:(NSMutableArray *)dataArray {
    XJLog(@"帖子数:%ld",dataArray.count);
    
    self.topicsProxy.dataArray = dataArray;
}


- (void)xjscTopicsFormatWillDeleteSelectedTopics:(NSArray *)selectedArr {
    XJLog(@"将要删除的数据源%ld",selectedArr.count);
    if (selectedArr.count == 0) {
        [JRToast showWithText:@"请选择要删除的帖子"];
        return;
    }
    
    
    [self.topicsFormat deleteTopics:selectedArr];
}

//页面需要刷新时会走此代理方法
- (void)xjscTopicsFormatReloadWhenNeed {
    [self.tableView reloadData];
}


#pragma mark - 四 编辑
- (void)setSctopicvcIsEditing:(BOOL)sctopicvcIsEditing {
    _sctopicvcIsEditing = sctopicvcIsEditing;

    XJLog(@"=====:%@",sctopicvcIsEditing ? @"YES":@"NO");

    //底部弹出view
    [self showAndHideBottomAnimaton:sctopicvcIsEditing];
    
    //是否处于编辑状态
    self.topicsProxy.isEditing = sctopicvcIsEditing;
    [self.tableView reloadData];
}


- (void)showAndHideBottomAnimaton:(BOOL)isEditing {
    if (isEditing) {
        [UIView animateWithDuration:0.25 animations:^{
            self.bottomView.frame = CGRectMake(0, KHeight - 40 - kStatusBarAndNavigationBarHeight - 50, KWidth, 50);
        }];
    }else {
        [UIView animateWithDuration:0.25 animations:^{
            self.bottomView.frame = CGRectMake(0, KHeight, KWidth, 50);
        }];
    }
}

@end
